﻿package com.petermacko.utils {
	public class ArrayUtil
	{
		//Determines whether the specified array contains the specified value.
		public static function arrayContainsValue(arr:Array, val:Object):Boolean {
			return (arr.indexOf(val) != -1);
		}	

		//Remove all instances of the specified value from the array,	
		public static function removeValueFromArray(arr:Array, val:Object):void {
			var l:uint = arr.length;
			for(var i:Number = l; i > -1; i--) {
				if(arr[i] === val) {
					arr.splice(i, 1);
				}
			}					
		}

		//Create a new array that only contains unique instances of objects in the specified array.
		public static function createUniqueCopy(a:Array):Array {
			var nA:Array = new Array();
			var l:Number = a.length;
			var it:Object;
			for (var i:uint = 0; i < l; ++i) {
				it = a[i];
				if(ArrayUtil.arrayContainsValue(nA, it)) {
					continue;
				}
				nA.push(it);
			}
			return nA;
		}

		//Creates a copy of the specified array.
		public static function copyArray(arr:Array):Array {	
			return arr.slice();
		}

		//Compares two arrays and returns a boolean indicating whether the arrays contain the same values at the same indexes.
		public static function arraysAreEqual(arr1:Array, arr2:Array):Boolean {
			if(arr1.length != arr2.length) {
				return false;
			}
			var l:Number = arr1.length;
			for(var i:Number = 0; i < l; i++) {
				if(arr1[i] !== arr2[i]) {
					return false;
				}
			}
			return true;
		}
	}
}